﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _003文字打怪游戏
{
    /// <summary>
    /// 角色基类
    /// </summary>
    class Character
    {
        private int _hp;//生命值       
        public int HP
        {
            get { return _hp; }
            set
            {
                if (value > 0)//如果需要对传入的数据进行处理，就只能写普通属性
                    _hp = value;
            }
        }
        //只读自动属性
        public string Name { get; }//名字
        //可读可写自动属性
        public float Defense { get; set; } = 5f;//防御值
        protected float AttackValue { get; set; } = 2f;//攻击力

        /// <summary>
        /// 基类构造函数
        /// </summary>
        /// <param name="name">名字</param>
        /// <param name="hp">血量</param>
        /// <param name="defense">防御值</param>
        /// <param name="av">攻击力</param>
        public Character(string name, int hp, float defense, float av)
        {
            //Console.WriteLine("Character基类4参构造方法被调用");
            Name = name;
            HP = hp;
            Defense = defense;
            AttackValue = av;
        }

        /// <summary>
        /// 攻击方法
        /// </summary>
        /// <param name="target"></param>
        public void Attack(Character target)
        {           
            Console.WriteLine("**********************");
            Console.WriteLine("{0}攻击了{1}",Name,target.Name);
            Console.WriteLine();
            target.TakeDamage(AttackValue);
        }
        /// <summary>
        /// 减血方法
        /// </summary>
        /// <param name="av">攻击力</param>
        public void TakeDamage(float av)
        {        
            HP -= (int)av;//减血
        }

        /// <summary>
        /// 输出角色属性
        /// 虚方法
        /// 父类希望子类改变该方法,不强迫
        /// </summary>
        public virtual void PrintCharacterAttr()
        {
            Console.WriteLine("-----角色属性-----");
            Console.WriteLine("名字:{0}",Name);
            Console.WriteLine("血量:{0}", HP);
            Console.WriteLine("防御值:{0}", Defense);
            Console.WriteLine("攻击力:{0}", AttackValue);
        }

    }
}
